# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ccache
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.2.5
pkgrel=1
pkgdesc="A compiler cache (mingw-w64)"
arch=('any')
url="https://ccache.samba.org/"
license=("GPL3")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' 'strip')
source=(https://samba.org/ftp/ccache/${_realname}-${pkgver}.tar.bz2{,.asc}
        "MinGW-add-ifdeds.patch"
        "Revert-Handle-some-more-stale-NFS-file-handle-cases.patch")
sha256sums=('7a553809e90faf9de3a23ee9c5b5f786cfd4836bf502744bedb824a24bee1097'
            'SKIP'
            '30977bff713a10de0392aeeff86f8aa09f13ccf76823edb985b028790d0da472'
            'c21342e871c1199eb2be9c11904a034566f59dad135a73e4f60a39e925ae6d5c')

prepare() {
  cd ${_realname}-${pkgver}
  #./autogen.sh

  #backported patch from git; fixed in unreleased 3.3
  patch -p1 -i ${srcdir}/MinGW-add-ifdeds.patch
  #MinGW-w64 errno.h doesn't define "ESTALE", revert upstream patch
  patch -p1 -i ${srcdir}/Revert-Handle-some-more-stale-NFS-file-handle-cases.patch
}

build() {
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST}  && cd build-${MINGW_CHOST}
  ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST}
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  # hack: use bash scripts as shortcuts since we cannot use symlinks
  install -d ${pkgdir}${MINGW_PREFIX}/lib/ccache/bin
  cd ${pkgdir}${MINGW_PREFIX}/lib/ccache/bin
  scripts=(c++ cc cpp gcc g++ ${MINGW_CHOST}-g++ ${MINGW_CHOST}-c++ ${MINGW_CHOST}-gcc)
  for fn in ${scripts[*]}; do
    echo -e '#!/bin/bash\n\nccache '${MINGW_PREFIX}'/bin/'$fn' "$@"' > $fn
  done
}
